package com.powerfl.frm.taf{
	
	import flash.events.*;
	import flash.media.*;
	import flash.net.*;
	import flash.utils.*;
	
	final public class TAFsound{
		
		internal var _sound:Sound;
		internal var _cn:SoundChannel;
		
		private var _loop:int;
		private var _playStat:int;
		private var _pausePosition:Number;
		
		public function TAFsound():void{}
		
		internal function collect():TAFsound{
			stop();
			_sound = null;
			_pausePosition = _playStat = _loop = 0;
			return this;
		}
		public function isPlaying():Boolean{
			return _playStat === 1;
		}
		public function duration():Number{
			return _sound.length;
		}
		public function current():Number{
			if( _cn ){
				return _cn.position;
			}else{
				return 0;
			}
		}
		public function play( $times:int = 1 ):void{
			if( _playStat === 0 ){
				_loop = $times;
				if( _loop < 1 ){
					_loop = int.MAX_VALUE;
				}
				_cn = _sound.play( 0, _loop );
				checkCN();
			}
		}
		public function pause():void{
			if( _playStat === 1 ){
				_playStat = 2;
				_pausePosition = _cn.position;
				stopCN();
			}
		}
		public function resume():void{
			if( _playStat === 2 ){
				_cn = _sound.play( _pausePosition, _loop );
				checkCN();
			}
		}	
		public function stop( $e:Event = null ):void{
			if( _playStat === 1 ){
				_playStat = 0;
				stopCN();
			}
		}
		private function checkCN():void{
			if( _cn ){
				_playStat = 1;
				_cn.addEventListener( Event.SOUND_COMPLETE, stop );
			}else{
				_playStat = 0;
			}
		}
		private function stopCN():void{
			_cn.stop();
			_cn.removeEventListener( Event.SOUND_COMPLETE, stop );
			_cn = null;
		}
	}
}